home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / demo / rotplnnr.lha / RouteEdit_mem.c < prev    next >
C/C++ Source or Header  |  1994-08-21  |  9KB  |  423 lines

  1. // RouteEdit_mem.c -- edits RoutePlanner files
  2.  
  3. #include <proto/asl.h>
  4. #include <proto/dos.h>
  5. #include <proto/exec.h>
  6. #include <proto/icon.h>
  7. #include <proto/utility.h>
  8. #include <proto/muimaster.h>
  9. #include <proto/intuition.h>
  10.  
  11. #include <clib/alib_protos.h>
  12.  
  13. #include <exec/memory.h>
  14. #include <utility/hooks.h>
  15.  
  16. #include <libraries/mui.h>
  17. #include <libraries/gadtools.h>
  18.  
  19. #include "RoutePlanner.h"
  20. #include "RouteEdit_rev.h"
  21.  
  22. #include <dos.h>
  23.  
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27.  
  28. #include <reg.h>
  29.  
  30. extern struct Library *MUIMasterBase;
  31. extern struct Library *IconBase;
  32. extern struct Library *UtilityBase;
  33. extern struct IntuitionBase *IntuitionBase;
  34. extern struct Library *AslBase;
  35.  
  36. extern BOOL Authenticated;
  37.  
  38. extern APTR app, memptr, cdptr, rdptr, mainwindow;
  39. extern struct DiskObject *disko;
  40.  
  41. // These two pointers used by memory reallocation system
  42. extern struct CityData *cd;
  43. extern struct RouteData *rd;
  44. extern ULONG cdcount,rdcount;
  45.  
  46. extern struct DataBaseInfo dbi;
  47.  
  48. extern struct Registration *reg;
  49.  
  50. extern UWORD MeasureUnits;
  51.  
  52. BOOL OpenDatabase(char * );
  53.  
  54. BOOL SaveDatabase(char * );
  55.  
  56. BOOL RedoMemory(ULONG , ULONG );
  57.  
  58. void ReLink(void);
  59.  
  60. BOOL OpenDatabase(char *filename)
  61. {
  62.   struct DataBaseInfo *dbitmp;
  63.   APTR tmemptr;
  64.   BPTR fhandle;
  65.   __aligned struct FileInfoBlock fib;
  66.   ULONG *temp;
  67.   
  68.   fhandle=Open(filename,MODE_OLDFILE);
  69.   if(!fhandle) return FALSE;
  70.   
  71.   if(!ExamineFH(fhandle,&fib))
  72.     {
  73.     Close(fhandle);
  74.     return FALSE;
  75.     }
  76.   
  77.   tmemptr=malloc(fib.fib_Size);
  78.   if(!tmemptr)
  79.     {
  80.     Close(fhandle);
  81.     return FALSE;
  82.     }
  83.   
  84.   FRead(fhandle,tmemptr,1,fib.fib_Size);
  85.   Close(fhandle);
  86.   
  87.   temp=(ULONG *)tmemptr;
  88.   if((*temp)!='RPL2')
  89.     {
  90.     return FALSE;
  91.     }
  92.   
  93.   RedoMemory(0L,0L);
  94.   memptr=tmemptr;
  95.   cdptr=NULL;
  96.   rdptr=NULL;
  97.   
  98.   cd=(struct CityData *)((char *)memptr+12);
  99.   temp=(ULONG *)((char *)(cd)-4);
  100.   cdcount=*temp;
  101.   
  102.   rd=(struct RouteData *)
  103.     ((char *)memptr+(sizeof(struct CityData)*(cdcount)+20));
  104.   
  105.   temp=(ULONG *)((char *)(rd)-4);
  106.   rdcount=*temp;
  107.   
  108.   dbitmp=(struct DataBaseInfo *)((char *)memptr+20+
  109.    (sizeof(struct CityData)*cdcount)+(sizeof(struct RouteData)*rdcount));
  110.   
  111.   memcpy(&dbi,dbitmp,sizeof(dbi));
  112.   
  113.   if(reg)
  114.     {
  115.     if((dbi.dbi_AuthorCode!=(ULONG)~0 && dbi.dbi_AuthorCode!=reg->ActualNumber) 
  116.       || dbi.dbi_Security!=DoFunkyThing(cdcount+rdcount+dbi.dbi_AuthorCode+
  117.       StringSum(dbi.dbi_Author)+StringSum(dbi.dbi_EMail)))
  118.       {
  119.       RedoMemory(0L,0L);
  120.       cdcount=0;
  121.       rdcount=0;
  122.       memset(&dbi,'\0',sizeof(dbi));
  123.       
  124.       MUI_Request(app,mainwindow,0,NULL,"_Ok",
  125.         "You may not open this database.");
  126.       }
  127.     }
  128.   else
  129.     {
  130.     if(dbi.dbi_AuthorCode!=(ULONG)~0 || 
  131.       dbi.dbi_Security!=DoFunkyThing(cdcount+rdcount+dbi.dbi_AuthorCode+
  132.       StringSum(dbi.dbi_Author)+StringSum(dbi.dbi_EMail)))
  133.       {
  134.       RedoMemory(0L,0L);
  135.       cdcount=0;
  136.       rdcount=0;
  137.       memset(&dbi,'\0',sizeof(dbi));
  138.       
  139.       MUI_Request(app,mainwindow,0,NULL,"_Ok",
  140.         "You may not open this database.");
  141.       }
  142.     }
  143.   
  144.   ReLink();
  145.   
  146.   return TRUE;
  147. }
  148.  
  149. BOOL SaveDatabase(char *filename)
  150. {
  151.   BPTR fhandle;
  152.   ULONG id='RPL2', cid='CITY', rid='ROAD';
  153.   LONG err, i;
  154.   static char buffer[100], *header="Error writing file";
  155.   struct DiskObject *tdisko;
  156.   
  157.   fhandle=Open(filename,MODE_NEWFILE);
  158.   if(!fhandle) return FALSE;
  159.   
  160.   SetIoErr(0L);
  161.   FWrite(fhandle,&id,sizeof(id),1);
  162.   err=IoErr();
  163.   if(err)
  164.     {
  165.     Fault(err,header,buffer,sizeof(buffer));
  166.     MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
  167.     Close(fhandle);
  168.     return FALSE;
  169.     }
  170.   
  171.   SetIoErr(0L);
  172.   FWrite(fhandle,&cid,sizeof(cid),1);
  173.   err=IoErr();
  174.   if(err)
  175.     {
  176.     Fault(err,header,buffer,sizeof(buffer));
  177.     MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
  178.     Close(fhandle);
  179.     return FALSE;
  180.     }
  181.   
  182.   SetIoErr(0L);
  183.   FWrite(fhandle,&cdcount,sizeof(cdcount),1);
  184.   err=IoErr();
  185.   if(err)
  186.     {
  187.     Fault(err,header,buffer,sizeof(buffer));
  188.     MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
  189.     Close(fhandle);
  190.     return FALSE;
  191.     }
  192.   
  193.   for(i=0;i<cdcount;i++)
  194.     {
  195.     cd[i].RoadList=NULL;
  196.     cd[i].Colum=0;
  197.     cd[i].cd_Dummy=0;
  198.     }
  199.   
  200.   SetIoErr(0L);
  201.   FWrite(fhandle,&cd[0],sizeof(struct CityData),cdcount);
  202.   err=IoErr();
  203.   if(err)
  204.     {
  205.     Fault(err,header,buffer,sizeof(buffer));
  206.     MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
  207.     ReLink();
  208.     Close(fhandle);
  209.     return FALSE;
  210.     }
  211.   
  212.   SetIoErr(0L);
  213.   FWrite(fhandle,&rid,sizeof(rid),1);
  214.   err=IoErr();
  215.   if(err)
  216.     {
  217.     Fault(err,header,buffer,sizeof(buffer));
  218.     MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
  219.     ReLink();
  220.     Close(fhandle);
  221.     return FALSE;
  222.     }
  223.   
  224.   SetIoErr(0L);
  225.   FWrite(fhandle,&rdcount,sizeof(rdcount),1);
  226.   err=IoErr();
  227.   if(err)
  228.     {
  229.     Fault(err,header,buffer,sizeof(buffer));
  230.     MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
  231.     ReLink();
  232.     Close(fhandle);
  233.     return FALSE;
  234.     }
  235.   
  236.   for(i=0;i<rdcount;i++)
  237.     {
  238.     rd[i].CityNum[0]=NULL;
  239.     rd[i].CityNum[1]=NULL;
  240.     rd[i].RLink[0]=NULL;
  241.     rd[i].RLink[1]=NULL;
  242.     }
  243.   
  244.   SetIoErr(0L);
  245.   FWrite(fhandle,&rd[0],sizeof(struct RouteData),rdcount);
  246.   err=IoErr();
  247.   if(err)
  248.     {
  249.     Fault(err,header,buffer,sizeof(buffer));
  250.     MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
  251.     ReLink();
  252.     Close(fhandle);
  253.     return FALSE;
  254.     }
  255.   
  256.   SetIoErr(0L);
  257.   FWrite(fhandle,&dbi,sizeof(dbi),1);
  258.   err=IoErr();
  259.   if(err)
  260.     {
  261.     Fault(err,header,buffer,sizeof(buffer));
  262.     MUI_Request(app,mainwindow,0,NULL,"_Ok",buffer);
  263.     ReLink();
  264.     Close(fhandle);
  265.     return FALSE;
  266.     }
  267.   
  268.   ReLink();
  269.   Close(fhandle);
  270.   
  271.   tdisko=GetDiskObject(filename);
  272.   
  273.   if(!tdisko)
  274.     {
  275.     tdisko=GetDiskObjectNew("PROGDIR:RoutePlanner");
  276.     if(tdisko)
  277.       {
  278.       tdisko->do_Type=WBPROJECT;
  279.       tdisko->do_DefaultTool="RoutePlanner";
  280.       tdisko->do_CurrentX=NO_ICON_POSITION;
  281.       tdisko->do_CurrentY=NO_ICON_POSITION;
  282.       
  283.       PutDiskObject(filename,tdisko);
  284.       }
  285.     else
  286.       {
  287.       tdisko=GetDefDiskObject(WBPROJECT);
  288.       if(tdisko)
  289.         {
  290.         tdisko->do_Type=WBPROJECT;
  291.         tdisko->do_DefaultTool="RoutePlanner";
  292.         tdisko->do_CurrentX=NO_ICON_POSITION;
  293.         tdisko->do_CurrentY=NO_ICON_POSITION;
  294.         
  295.         PutDiskObject(filename,tdisko);
  296.         }
  297.       }
  298.     }
  299.   
  300.   if(tdisko) FreeDiskObject(tdisko);
  301.   
  302.   return TRUE;
  303. }
  304.  
  305. BOOL RedoMemory(ULONG NeededCityEntries, ULONG NeededRouteEntries)
  306. {
  307.   APTR temp1, temp2;
  308.   BOOL ok;
  309.   
  310.   if(NeededCityEntries==0 && NeededRouteEntries==0)
  311.     {
  312.     if(memptr)
  313.       {
  314.       free(memptr);
  315.       memptr=NULL;
  316.       }
  317.     else
  318.       {
  319.       if(rdptr) free(rdptr);
  320.       if(cdptr) free(cdptr);
  321.       
  322.       cdptr=NULL;
  323.       rdptr=NULL;
  324.       }
  325.     
  326.     cd=NULL;
  327.     rd=NULL;
  328.     
  329.     ok=TRUE;
  330.     }
  331.   else if(NeededCityEntries!=cdcount || NeededRouteEntries!=rdcount)
  332.     {
  333.     if(memptr)
  334.       {
  335.       ULONG csize, rsize;
  336.       
  337.       csize=min(cdcount,NeededCityEntries)*sizeof(struct CityData);
  338.       rsize=min(rdcount,NeededRouteEntries)*sizeof(struct RouteData);
  339.       
  340.       temp1=malloc(NeededCityEntries*sizeof(struct CityData));
  341.       temp2=malloc(NeededRouteEntries*sizeof(struct RouteData));
  342.       
  343.       if((temp1 || cdcount==0) && (temp2 || rdcount==0))
  344.         {
  345.         CopyMem(cd,temp1,csize);
  346.         CopyMem(rd,temp2,rsize);
  347.         
  348.         free(memptr);
  349.         memptr=NULL;
  350.         
  351.         cdptr=temp1;
  352.         rdptr=temp2;
  353.         
  354.         ok=TRUE;
  355.         }
  356.       else
  357.         {
  358.         if(temp1) free(temp1);
  359.         if(temp2) free(temp2);
  360.         
  361.         ok=FALSE;
  362.         }
  363.       }
  364.     else
  365.       {
  366.       if(NeededCityEntries!=cdcount)
  367.         temp1=realloc(cdptr,NeededCityEntries*sizeof(struct CityData));
  368.       else
  369.         temp1=cdptr;
  370.       
  371.       if(NeededRouteEntries!=rdcount)
  372.         temp2=realloc(rdptr,NeededRouteEntries*sizeof(struct RouteData));
  373.       else
  374.         temp2=rdptr;
  375.       
  376.       if((temp1 || cdcount==0) && (temp2 || rdcount==0))
  377.         {
  378.         cdptr=temp1;
  379.         rdptr=temp2;
  380.         
  381.         ok=TRUE;
  382.         }
  383.       else
  384.         {
  385.         ok=FALSE;
  386.         }
  387.       }
  388.     
  389.     cd=cdptr;
  390.     rd=rdptr;
  391.     }
  392.   
  393.   return ok;
  394. }
  395.  
  396. void ReLink(void)
  397. {
  398.   char col;
  399.   ULONG i;
  400.   struct RouteData *Route;
  401.   struct CityData *FoundCity;
  402.   
  403.   for(i=0;i<cdcount;i++)
  404.     cd[i].RoadList=NULL;
  405.   
  406.   for(i=0;i<rdcount;i++)
  407.     {
  408.     Route=&(rd[i]);
  409.     
  410.     for(col=0;col<2;col++)
  411.       {
  412.       FoundCity=&(cd[Route->CityID[col]]);
  413.       
  414.       Route->CityNum[col]=FoundCity;
  415.       Route->RLink[col]=FoundCity->RoadList;
  416.       Route->Colm[col]=FoundCity->Colum;
  417.       FoundCity->RoadList=Route;
  418.       FoundCity->Colum=col;
  419.       }
  420.     }
  421. }
  422.  
  423.